<?php

namespace app\admin\controller;

use app\admin\logic\StatisticsLogic;
use support\Request;
use support\Response;

class StatisticsController extends BaseController
{
    /**
     * 构造
     */
    public function __construct()
    {
        $this->logic = new StatisticsLogic();
        parent::__construct();
    }

    /**
     * 获取货物列表
     * @param Request $request
     * @return Response
     */
    public function getCargoList(Request $request): Response
    {
        $where = $request->more([
            ['company_id', 0],
            ['dealer_id', 0]
        ]);
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);

        $cargoList = $this->logic->getCargoList($where, $page, $limit);
        return $this->success($cargoList);
    }

    public function getInventoryList(Request $request): Response
    {
        $where = $request->more([
            ['company_id', 0],
            ['dealer_id', 0],
            ['create_time', []]
        ]);
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);

        $inventoryList = $this->logic->getInventoryList($where, $page, $limit);
        return $this->success($inventoryList);
    }
    

    /**
     * 经验业绩报表
     */
    public function getPerformanceList(Request $request): Response
    {
        $where = $request->more([
            ['company_id', 0],
            ['dealer_id', 0],
            ['create_time', []]
        ]);
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);

        $performanceList = $this->logic->getPerformanceList($where, $page, $limit);
        return $this->success($performanceList);
    }

    /**
     * 业务数据报表
     */
    public function getBusinessDataList(Request $request): Response
    {
        $where = $request->more([
            ['company_id', 0],
            ['dealer_id', 0],
            ['create_time', []]
        ]);
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);

        $businessDataList = $this->logic->getBusinessDataList($where, $page, $limit);
        return $this->success($businessDataList);
    }

    /**
     * 业务详情
     */
    public function getBusinessDetail(Request $request): Response
    {
        $where = $request->more([
            ['company_id', 0],
            ['stat_date', '']
        ]);

        if (empty($where['company_id']) || empty($where['stat_date'])) {
            return $this->fail('参数缺失');
        }
        $page = $request->input('page', 1);
        $limit = $request->input('limit', 20);

        $businessDetail = $this->logic->getBusinessDetail($where, $page, $limit);
        return $this->success($businessDetail);
    }

    /**
     * 保存业务详情
     */
    public function saveBusinessDetail(Request $request): Response
    {
        $data = $request->input('data');
        if (empty($data) || !is_array($data)) {
            return $this->fail('数据无更新');
        }
        // 提取所有的 ID
        $ids = array_column($data, 'id');
        if (empty($ids)) {
            return $this->fail('没有提供有效的 ID');
        }

        $result = $this->logic->saveBusinessDetail($data);
        return $result === true ? $this->success('更新完成') : $this->fail('数据更新失败');
    }
}